package cn.wwcyc.controller;

import cn.wwcyc.domain.R;
import cn.wwcyc.domain.TjDev;
import cn.wwcyc.domain.TjReg;
import cn.wwcyc.domain.TjSpecialtype;
import cn.wwcyc.mapper.TjRegMapper;
import cn.wwcyc.mapper.TjSpecialtypeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reg")

public class REGController {

    @Autowired
    TjRegMapper tjRegMapper;

    @RequestMapping("/getAll")
    public R<List<TjReg>> getAll(
    ) {
        List<TjReg> list = tjRegMapper.selectList(null);
        return R.success(list);
    }

    //此分页函数中的返回对象中不含有单位及类型对象数据
    @RequestMapping("/getpage")
    public R<IPage<TjReg>> getPage(
            @RequestParam Integer pagenum,
            @RequestParam Integer pagesize,
            @RequestParam(required = false) Long devid,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long specialtypeid
    ) {
        LambdaQueryWrapper<TjReg> qw = new LambdaQueryWrapper();
        if (!StringUtils.isBlank(name))
            qw.like(TjReg::getName, name);
        if (devid != null)
            qw.eq(TjReg::getDevid, devid);
        if (specialtypeid != null)
            qw.eq(TjReg::getSpecialtypeid, specialtypeid);
        IPage<TjReg> page = new Page<>(pagenum, pagesize);
        page = tjRegMapper.selectPage(page, qw);
        return R.success(page);
    }

    @RequestMapping("/getpageWithObj")
    public R<IPage<TjReg>> getPageWithObj(
            @RequestParam Integer pagenum,
            @RequestParam Integer pagesize,
            @RequestParam(required = false) Long devid,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long specialtypeid
    ) {
       IPage<TjReg> page = new Page<>(pagenum, pagesize);
        QueryWrapper<TjReg> qw = new QueryWrapper<>();
        if (!StringUtils.isBlank(name)) {
            qw.like("name", name);
        }
        if (devid != null) {
            qw.eq("devid", devid);
        }
        if (specialtypeid != null) {
            qw.eq("devtype", specialtypeid);
        }
        long count = tjRegMapper.selectCount(qw);

        page.setTotal(count);
        page.setPages(page.getTotal() % page.getSize() == 0 ? page.getTotal() / page.getSize() : page.getTotal() / page.getSize() + 1);
        if (pagenum > page.getPages()) {
            page.setCurrent(page.getPages());
        }
        if (pagenum < 1) {
            page.setCurrent(1);
        }
//        System.out.println("pagenum:" + pagenum + "  pagesize:" + pagesize + " current:" + page.getCurrent() + "  size:" + page.getSize() + " pages:" + page.getPages());
        List<TjReg> list = tjRegMapper.selectPagewithObj(page.getCurrent(), page.getSize(), devid, specialtypeid, name);
        page.setRecords(list);
        return R.success(page);
    }


    @RequestMapping("/getByDevid")
    public R<List<TjReg>> getByDevid(
            @RequestParam(required = false) Long devid
    ) {
        LambdaQueryWrapper<TjReg> qw = new LambdaQueryWrapper();

        if (devid != null)
            qw.eq(TjReg::getDevid, devid);

        List list = tjRegMapper.selectList( qw);
        return R.success(list);
    }

    @RequestMapping("/getByDevidWithObj")
    public R<List<TjReg>> getByDevidWithObj(
            @RequestParam(required = false) Long devid
    ) {

        List list = tjRegMapper.selectListWithObj(devid );
        return R.success(list);
    }

    @PostMapping("/save")
    public R save(@RequestBody TjReg reg) {
        tjRegMapper.insert(reg);
        return R.success();
    }

    @PostMapping("/edit")
    public R edit(@RequestBody TjReg reg) {
        tjRegMapper.updateById(reg);
        return R.success();
    }

    //
    @DeleteMapping
    public R del(Integer id) {
        tjRegMapper.deleteById(id);
        return R.success();
    }

    @PostMapping("delbatch")
    public R delbatch(@RequestBody List<TjReg> list) {
        for (TjReg reg : list) {
            Long id = reg.getId();
            tjRegMapper.deleteById(id);
        }
        return R.success();
    }
}
